package ua.kpi_java_training.evgeniygusar.module2;

public class Predator extends Fish {

    int NUMBERS_STEPS_OF_PREDATOR = 5;
    int PREDATOR_BIRTH_CYCLE = 3;
    public int x_of_nearest_victim, y_of_nearest_victim;
    int index_of_nearest_victim = 0;

    AllVictims victimsObject = new AllVictims();
    AllPredators predatorsObject = new AllPredators();

    int getPredatorBirthCycle() {
        return PREDATOR_BIRTH_CYCLE;
    }

    public void Moving() {
        int current_step=1;
        victimsObject.nearestVictim(x, y);
        x_of_nearest_victim = victimsObject.getXOfNearestVictim();
        y_of_nearest_victim = victimsObject.getYOfNearestVictim();
        index_of_nearest_victim = victimsObject.getIndexOfNearestVictim();

        while ((current_step <= NUMBERS_STEPS_OF_PREDATOR)&(victimsObject.numbersOfVictims() != 0)) {
            if ((current_step%2 != 0) ) {
                if (x != x_of_nearest_victim)
                    stepX();
                else stepY();
                displayCurrentStep(current_step);
                checkVictimIsEated();
            }
            else if ((current_step%2 == 0) ) {
                if (y != y_of_nearest_victim)
                    stepY();
                else stepX();
                displayCurrentStep(current_step);
                checkVictimIsEated();
            }
            current_step++;
        }
    }

    void displayCurrentStep(int current_step) {
        System.out.print("   Step " + current_step + ": ");
        showCoordinats();
    }

    public void checkVictimIsEated() {
        if((x == x_of_nearest_victim) & (y == y_of_nearest_victim)) {
            displayVictimIsEated();

            victimsObject.removeVictim(index_of_nearest_victim);
            predatorsObject.incrementTotalNumberOfEatedVictims();
            if (victimsObject.numbersOfVictims() != 0) {
                victimsObject.nearestVictim(x, y);
                x_of_nearest_victim = victimsObject.getXOfNearestVictim();
                y_of_nearest_victim = victimsObject.getYOfNearestVictim();
                index_of_nearest_victim = victimsObject.getIndexOfNearestVictim();
            }
        }
    }

    void displayVictimIsEated() {
        System.out.println("Niam-Niam!");
        System.out.println("Victim " + (index_of_nearest_victim+1) +
                " with coordinats: x = " + x_of_nearest_victim +
                ", y = " + y_of_nearest_victim + " is eated!");
    }

    public void stepX() {
        if (x > x_of_nearest_victim)
            x--;
        else if (x < x_of_nearest_victim)
            x++;
    }

    public void stepY() {
        if (y > y_of_nearest_victim)
            y--;
        else if (y < y_of_nearest_victim)
            y++;
    }
}
